package fileevent

import (
	"github.com/fsnotify/fsnotify"
	"os"
	"syscall"
	"os/user"
	"strconv"
	"cloud_worm/worm/client/monitor/monitorlog"
	"time"
)

type FileEvent struct {
	Type 	int 		`json:"type"`
	IP 		string 		`json:"ip"`
	Name	string		`json:"name"`
	Event 	string		`json:"event"`
	Mode 	string		`json:"mode"`
	User 	string		`json:"user"`

	Hash 	string 		`json:"hash"`
	TimeStamp 	string	`json:"time_stamp"`
}

func (f *FileEvent) Analyzer(ip string, ev fsnotify.Event) error {
	f.Type = monitorlog.FileLogType
	f.IP = ip
	f.Name = ev.Name
	f.Event = ev.Op.String()

	fInfo, err := os.Stat(f.Name)
	if err != nil {
		return err
	}
	f.Mode = fInfo.Mode().String()

	stat := fInfo.Sys().(*syscall.Stat_t)
	if u, err := user.LookupId(strconv.Itoa(int(stat.Uid))); err != nil {
		f.User = strconv.Itoa(int(stat.Uid))
	} else {
		f.User = u.Username
	}

	f.TimeStamp = time.Now().Format("2006-01-02 15:04:05")
	return nil
}
